var url = require('url');
var template = require('templateConfig');
var main = require('main');
var form = require('form');
var module = require('module');

//模板配置
var tplArr = [];
var modalTpl =require('raw!../template/modal.txt'); 
var detailTpl =require('raw!../template/product-detail.txt'); 
tplArr['modal'] = template.compile(modalTpl.replace(/^\s*|\s*$/g, ""));
tplArr['detail'] = template.compile(detailTpl.replace(/^\s*|\s*$/g, "")); 
 

function Detail(){
    _this = this;
    this.main = function(){
        main.event(this,'tap','i-click'); 
        this.init();
    } 
      
    this.getContent = function(id, url, postUrl){
        main.get(url,{product_id: id}, function(res){  
            var ret= res.data[0];
           console.info(res)
            var property={
                "color": ret.color,
                "cup": ret.cup_type_array,
                "size": ret.size_type_array 
            };
            var data ={
                tType: "content",
                title: ret.brand_name,
                subtitle: ret.product_name,
                attr: ret.value_img_array,
                price: ret.money,
                property: module.getModule(property, "property"),
                description: _this.html_encode(ret.description),
                pic: ret.good_img_array,  
                product_id: ret.product_id,
                postUrl: postUrl             
            }
            $(".content").html(tplArr['detail'](data));             
            //$("#html_section").html(ret.description);
            //document.getElementById("html_section").innerHTML= ret.description;
            //console.info(_this.html_encode(ret.description))
            _this.slider();            
        })    	
    }
    //解码
      this.html_encode= function(str){
        var s = "";
        if (str.length == 0) return "";
        s = str.replace(/&gt;/g, ">");
        s = s.replace(/&lt;/g, "<");
        s = s.replace(/ /g, " ");
        s = s.replace(/\'/g, "'");
        s = s.replace(/\"/g, '"');
        s = s.replace(/\n/g, "<br>");
        return s;
      }

    this.getFoot =function() {
        main.post(url.getCount,{},function(ret){
            console.info(ret)
            var data = {
                tType: "footer",
                active: "0",
                num: ret.data,
                cart: _this.jumpUrl,
                index: _this.indexUrl
            }
            $(".wrap").append(tplArr['detail'](data));
        }) 
    } 
    //轮播 
    this.slider = function(){
        var slider = new fz.Scroll('.ui-slider', {
            role: 'slider',
            indicator: true,
            autoplay: true,
            interval: 3000
        });
    }
    this.starClick= function(ths) {
        ths.toggleClass("on");
    }

    //修改数量
    this.changeCount= function(ths, type){
        var num= parseInt(ths.siblings(".quantity").val());
        if(type== 1){
            ths.siblings(".quantity").val(++num);
        }
        else if(num> 1) {
            ths.siblings(".quantity").val(--num);
        }
    }

    //加入购物车
    this.addCart= function(ths, type){ 
        _this.addCarttype= type;
        form.ajaxSubmit(".postForm",_this,ths);
    }
    this.formCallback= function(ret){
        if(ret.code== 1){
            console.info(_this.addCarttype)
            if(_this.addCarttype== 0){
                window.location.href= _this.jumpUrl;
            }
            else {
                main.alert({
                        "title": "提示",
                        "content": "加入购物车成功!",
                  });

                var _n= parseInt($(".ui-badge-cornernum").html());
                _n++;
                $(".ui-badge-cornernum").html(_n);
            }
        }
    }

    this.init= function(){ 
        this.productid= main.GetQueryString("id");
        this.getUrl= url.getProductInfo;
        this.postUrl= url.postAddCart;
        this.jumpUrl= "shop-cart";
        this.indexUrl= "index";
        this.getFoot();  
        this.getContent(this.productid, this.getUrl, this.postUrl); 
    }
    return this.main();
};

var detail = new Detail();